#pragma once
#include<assert.h>
#include"reverse_iterator.h"
namespace xjh
{
	//链表结点
	template<class T>
	struct ListNode
	{
		//构造函数：
		ListNode(const T& date = T())
		:_Prev(nullptr),
		_Next(nullptr),
		_date(date)
		{}
					
		ListNode<T>* _Prev;
		ListNode<T>* _Next;
		T _date;		
	};
	//这个迭代器是给list容器里面使用的；
	//第二个模板参数的巧妙在于：
	//通过传该引用：可以传const 和 非 const 的类型过去
	//这样一份代码解决了const 和 非 const 的问题
	template<class T,class Ref,class Ptr>
	struct __list_iterator
	{
		typedef ListNode<T> Node;
		//构造函数
		__list_iterator(Node* p) :_node(p)
		{}
		
		// it !=it.end()
		bool operator!=(const __list_iterator<T,Ref,Ptr>& it) const
		{
			return _node != it._node;
		}
		bool operator ==(const __list_iterator<T, Ref, Ptr>& it) const
		{
			return _node == it._node;
		}
		Ref operator*()
		{
			return _node->_date;
		}
		Ptr operator->()
		{
			return &_node->_date;
		}
		//++it 等价 it.operator++()
		__list_iterator<T, Ref, Ptr>& operator++()
		{
			_node = _node->_Next;
			return *this;
		}
		__list_iterator<T, Ref, Ptr> operator++(int)
		{
			__list_iterator<T, Ref, Ptr> temp(*this);
			++*this;
			return temp;
		}
		//--it
		__list_iterator<T, Ref, Ptr>& operator--()
		{
			_node = _node->_Prev;
			return *this;
		}
		__list_iterator<T, Ref, Ptr> operator--(int)
		{
			__list_iterator<T, Ref, Ptr> temp(*this);
			--*this;
			return temp;
		}
		//成员数据
		Node* _node;
	};
	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		typedef __list_iterator<T,T&,T*> iterator;
		typedef __list_iterator<T, const T&,const T*> const_iterator;

		typedef reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
		typedef reverse_iterator<iterator, T&, T*> reverse_iterator;


	public:
		//构造函数
		list()
		{
			_head = new Node;
			_head->_Next = _head;
			_head->_Prev = _head;
		}
		list(int n, const T& val = T())
		{
			_head = new Node;
			_head->_Next = _head;
			_head->_Prev = _head;
			for (int i = 0; i < n; ++i)
			{
				push_back(val);
			}
		}
		//区间构造
		template<class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			_head = new Node;
			_head->_Next = _head;
			_head->_Prev = _head;

			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		//传统的拷贝构造和赋值
		/*list(const list<T>& lt)
		{
			list();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}
		list<T>& operator=(const list<T>& lt)
		{
			if (this != &lt)
			{
				clear();
				for (auto& e : lt)
				{
					push_back(e);
				}
			}
			return *this;
		}*/
		//现代版的拷贝构造和赋值

		list(const list<T>& lt)
		{
			_head = new Node;
			_head->_Next = _head;
			_head->_Prev = _head;
			list<T> temp(lt.begin(), lt.end());
			std::swap(_head, temp._head);			
		}

		list<T>& operator== (list<T> lt)
		{
			std::swap(_head, lt._head);
			return *this;
		}

		//尾插尾删头插头删
		void push_back(const T& x)
		{		
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}
		//pos前面插入一个数据
		iterator insert(iterator pos, const T& x)
		{
			Node* newNode = new Node(x);
			Node* cur = pos._node;
			Node* prev = cur->_Prev;
			
			prev = newNode;
			newNode->_Prev = prev;
			cur->_Prev = newNode;
			newNode->_Next = cur;

			return iterator(newNode);
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());

			Node* prev = pos._node->_Prev;
			Node* next = pos._node->_Next;

			delete pos._node;

			prev->_Next = next;
			next->_Prev = prev;

			return iterator(next);
		}
		iterator begin()
		{
			return iterator(_head->_Next);
		}
		iterator end()
		{
			return iterator(_head);
		}
		const_iterator begin() const
		{
			return const_iterator(_head->_Next);
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}
		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		const_reverse_iterator rbegin() const
		{
			return const_reverse_iterator(end());
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}
		const_reverse_iterator rend() const
		{
			return const_reverse_iterator(begin());
		}
		//清除结点，头结点还保存
		/*void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				iterator pos = it++;
				delete pos._node;
			}
			_head->_Next = _head;
			_head->_Prev = _head;
		}*/
		void clear()
		{
			iterator it = begin();

			while (it != end())
			{
				erase(it++);
			}
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
	private:
		Node* _head;
	};
}